﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.EHR.Library;
using ZX.BLL.Extend;
using ZX.DBModule;
using ZX.Web.UI.Controls;
using ZX.Common;
using System.Text;

namespace ZX.WebApp.EditWidows.OrganizationalManager
{
    public partial class CreatDepartment : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string deptId = this.GetStringFromQueryString("DeptId", "");
            BaseEntity instance = ZX.Common.Globals.getEntity("DatDepartment") as BaseEntity;
            BaseEntity postionInstance = ZX.Common.Globals.getEntity("DatPost") as BaseEntity;
            //修改部门
            if (deptId != string.Empty)
            {
                BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
                BaseEntity entity = bInstance.GetEntityById(deptId) as BaseEntity;
                this.EditTemplate.Text = CreateHtml(entity, null, "dept");
                IsEdit = "1";
                //BaseBussnies BpostionInstance = new BLL.Extend.BaseBussnies(postionInstance);

                //BaseEntity positionEntity = BpostionInstance.GetEntityById(entity) as BaseEntity;

            }
            else
            {
                //新增部门
                this.EditTemplate.Text = CreateHtml(instance, null, "dept");
                IsEdit = "0";
                this.EditTemplate1.Text = CreateHtml(postionInstance, null, "Post");
                //BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
                //IList<EditUIConfig> list = bInstance.GetEntitys<EditUIConfig>("EntityName='DatDepartment'");
            }
         }
        private string _entityJson;
        public string entityJson
        {
            get
            {
                return _entityJson; 
            }
            set
            {
                _entityJson = value;
            }
        }
        //标识是否为编辑页
        private string _isEdit;
        public string  IsEdit
        {
            get
            {
               return _isEdit;
            }
            set
            {
                _isEdit = value;
            }
        }

        //得到一个随机的ID
        public Guid getGuid
        {
            get
            {
                return Guid.NewGuid();
            }
        }
        //获取组织架构码
        public string DeptPath
        {
            get
            {
                if (IsEdit != "1")
                {
                    ZX.BLL.BDept bll = new ZX.BLL.BDept();
                    Guid parentId = new Guid(Request.QueryString["parentId"]);
                    return bll.CreateNewTreeCode(parentId);
                }
                return "";
            }
        }
        /// <summary>
        /// 生成界面的HTML
        /// </summary>
        /// <param name="instance">实例化</param>
        /// <param name="index">实例序号</param>
        /// <param name="entity">New Entity</param>
        /// <returns></returns>
        public virtual string CreateHtml(BaseEntity instance, BaseEntity entity,string  entityName)
        {
            if (entity == null)
            {
                if (!(instance is DataTableBaseEntity))
                {
                    return ("var instance" + entityName + "=[" + instance.ToJSON() + "];");
                }
                else
                {
                    return ("var instance" + entityName + "=[" + (instance as DataTableBaseEntity).EntityJson + "];");
                }

            }
            else
            {
                if (!(entity is DataTableBaseEntity))
                {
                    return ("var instance=[" + entity.ToJSON() + "];");
                }
                else
                {
                    return ("var instance=[" + (entity as DataTableBaseEntity).EntityJson + "];");
                }
                //return ("var instance=[" + entity.ToJSON().Replace("\n", "[BR]").Replace("\r", "") + "];");
            }
        }


    }
}